//
//  DMMessageCell.m
//  BaseAPP
//
//  Created by Evisu on 17/4/15.
//  Copyright © 2017年 evisum. All rights reserved.
//

#import "DMMessageCell.h"
#import "EMTextMessageBody.h"
#import "EMImageMessageBody.h"
#import "TextMessageView.h"

@interface DMMessageCell ()

//头像
@property (nonatomic, strong)UIImageView *ivHeadImage;

//气泡背景层
@property (nonatomic, strong)UIImageView *bubbleView;

//文字
@property (nonatomic, strong)UILabel *label;

//图片View
@property (nonatomic, strong)UIImageView *ivPhotoView;


@end

@implementation DMMessageCell

- (UIImageView *)ivHeadImage {
    
    if (!_ivHeadImage) {
        
        _ivHeadImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"headImage.jpg"]];
    }
    
    return _ivHeadImage;
}

- (UIImageView *)bubbleView {
    
    if (!_bubbleView) {
        
        _bubbleView = [[UIImageView alloc] init];
    }
    
    return _bubbleView;
}

- (UILabel *)label {
    
    if (!_label) {
        
        _label = [[UILabel alloc] init];
        _label.numberOfLines = 0;
        _label.font = [UIFont systemFontOfSize:14.0];
        _label.textColor = HEXColor(@"020203");
    }
    
    return _label;
}

- (UIImageView *)ivPhotoView {
    
    if (!_ivPhotoView) {
        
        _ivPhotoView = [[UIImageView alloc] init];
    }
    
    return _ivPhotoView;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self initViews];
        
    }
    
    return self;
}

- (void)initViews {
    
    self.backgroundColor = HEXColor(@"ebebebx");
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [self.contentView addSubview:self.ivHeadImage];
    //    [self.bubbleView addSubview:self.ivPhotoView];
    
    //处理点击事件
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapActions:)];
    
    [self.contentView addGestureRecognizer:tap];
}

- (void)layoutViews {
    
    
}

- (void)setCellModel:(DMMessageCellModel *)cellModel {
    
    _cellModel = cellModel;
    DMMessageModel *message = cellModel.message;
    self.message = message;
    
    _cellModel = [[DMMessageCellModel alloc] init];
    _cellModel.message = message;
    
    self.ivHeadImage.frame = _cellModel.headImageFrame;
    self.bubbleView.frame = _cellModel.bubbleFrame;
    self.label.frame = _cellModel.labelFrame;
    
    
    if (message.direction == MessageDirectionReceive) {
        
        //气泡背景图
        UIImage *bubbleImage = [UIImage imageNamed:@"message_receiver_background_normal"];
        bubbleImage = [bubbleImage resizableImageWithCapInsets:UIEdgeInsetsMake(bubbleImage.size.height*0.6, bubbleImage.size.width*0.5, bubbleImage.size.height*0.3, bubbleImage.size.width*0.5) resizingMode:UIImageResizingModeStretch];
        self.bubbleView.image = bubbleImage;
        
        /******************接收消息*****************/
        switch (message.type) {
            case MessageTypeText:
            {
                //文字消息
                [self.contentView addSubview:self.bubbleView];
                [self.bubbleView addSubview:self.label];
                [self.ivPhotoView removeFromSuperview];
                
                self.label.text = message.text;
                
                
            }
                break;
                
            case MessageTypeImage:{
                //图片消息
                [self.contentView addSubview:self.ivPhotoView];
                [self.bubbleView removeFromSuperview];
                
                self.ivPhotoView.image = [UIImage imageWithContentsOfFile:message.thumbnailLocalPath];
                CALayer *layer = self.bubbleView.layer;
                layer.frame = CGRectMake(0, 0, self.ivPhotoView.bounds.size.width, self.ivPhotoView.bounds.size.height);
                self.ivPhotoView.layer.mask = layer;
                [self.ivPhotoView setNeedsDisplay];
                
                self.ivPhotoView.frame = _cellModel.photoViewFrame;
                self.bubbleView.frame = _cellModel.bubbleFrame;
                
            }
                break;
                
            default:
                break;
        }
        
        
    } else if (message.direction == MessageDirectionSend) {
        
        /******************发送消息*****************/
        
        //气泡背景图
        UIImage *bubbleImage = [UIImage imageNamed:@"message_sender_background_normal"];
        bubbleImage = [bubbleImage resizableImageWithCapInsets:UIEdgeInsetsMake(bubbleImage.size.height*0.6, bubbleImage.size.width*0.5, bubbleImage.size.height*0.3, bubbleImage.size.width*0.5) resizingMode:UIImageResizingModeStretch];
        self.bubbleView.image = bubbleImage;
        
        switch (message.type) {
            case MessageTypeText:
            {
                //文字消息
                [self.contentView addSubview:self.bubbleView];
                [self.bubbleView addSubview:self.label];
                [self.ivPhotoView removeFromSuperview];
                
                self.label.text = message.text;
            }
                break;
                
            case MessageTypeImage:{
                //图片消息
                [self.contentView addSubview:self.ivPhotoView];
                [self.bubbleView removeFromSuperview];
                
                self.ivPhotoView.image = [UIImage imageWithContentsOfFile:message.thumbnailLocalPath];
                CALayer *layer = self.bubbleView.layer;
                layer.frame = CGRectMake(0, 0, self.ivPhotoView.bounds.size.width, self.ivPhotoView.bounds.size.height);
                self.ivPhotoView.layer.mask = layer;
                [self.ivPhotoView setNeedsDisplay];
                
                self.ivPhotoView.frame = _cellModel.photoViewFrame;
                self.bubbleView.frame = _cellModel.bubbleFrame;
                
            }
                break;
                
            default:
                break;
        }
    }
}

/**
 *  头像、消息、空白的点击事件
 */
- (void)handleTapActions:(UIGestureRecognizer *)tap {
    
    if (tap.state == UIGestureRecognizerStateEnded) {
        
        CGPoint tapPoint = [tap locationInView:self.contentView];
        
        if (CGRectContainsPoint(self.ivHeadImage.frame, tapPoint)) {
            
            DMLog(@"点击头像");
        } else if (CGRectContainsPoint(self.ivPhotoView.frame, tapPoint)  && _message.type == MessageTypeImage) {
            
            DMLog(@"点击图片");
            
        } else if (CGRectContainsPoint(self.bubbleView.frame, tapPoint) && _message.type == MessageTypeText) {
            
            DMLog(@"点击文字");
            if ([self.tapDelegate respondsToSelector:@selector(messageCellDidTapBlank)]) {
                
                [self.tapDelegate messageCellDidTapBlank];
            }
            
        } else {
            
            DMLog(@"点击空白");
            if ([self.tapDelegate respondsToSelector:@selector(messageCellDidTapBlank)]) {
                
                [self.tapDelegate messageCellDidTapBlank];
            }
        }
        
    }
}

@end
